package unit8.part3;

import javax.swing.*;
import java.awt.*;

public class Calculator2 {
    public static void main(String[] args) {
        CalculatorFrame calculatorFrame = new CalculatorFrame();
        calculatorFrame.setSize(400, 300);
        calculatorFrame.setVisible(true);
    }
}

class CalculatorFrame extends JFrame {  // 自定义窗体
    JTextField textField;
    KeysPanel keyPanel;

    public CalculatorFrame() {  // 窗体构造方法
        setTitle("测试计算器");

        textField = new JTextField(20);
        textField.setFont(new Font("微软雅黑", Font.BOLD, 24));
        add(textField, BorderLayout.NORTH);

        String[] keys = new String[]{"7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "-", "0", ".", "=", "+"};
        keyPanel = new KeysPanel(4, 4, keys);
        add(keyPanel, BorderLayout.CENTER);
    }
}

class KeysPanel extends JPanel {// 自定义面板
    JButton[] keyButtons;

    public KeysPanel(int rows, int column, String[] keys) {
        setLayout(new GridLayout(rows, column));

        keyButtons = new JButton[16];
        for (int i = 0; i < keys.length; i++) {
            keyButtons[i] = new JButton(keys[i]);
            add(keyButtons[i]);
        }
    }
}